﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microhard.CustomActivities.Design;
using System.Activities;
using Microhard.Core;
using Microhard.ApproveFlow.Buz.DataAccess;

namespace Microhard.CustomActivities.Activities
{
    [Designer(typeof(MHSingleApproverActivity))]
    public class SingleApproverActivity : MicrohardActivity
    {
        private MicroHardApproveFlowEntities db = null;
        public SingleApproverActivity()
        {
            db = new MicroHardApproveFlowEntities();
        }

        protected override void Execute(System.Activities.NativeActivityContext context)
        {
            //提醒当前节点的审批人
            var wfid = context.WorkflowInstanceId;
            var wfInstance = db.t_WorkflowDyncStatus.SingleOrDefault(s => s.InstanceID == wfid);
            //发送消息
            db.t_WorkflowNotificationMsg.AddObject(new t_WorkflowNotificationMsg
            {
                CreateTime = DateTime.Now,
                ToUserID = base.ApproverID,
                ToUserName = base.ApproverName,
                Msg = "你有一个待办流程,名称" + wfInstance.Title,
                WorkflowMsgID = Guid.NewGuid()
            });
            try
            {
                db.SaveChanges();
                context.CreateBookmark("SingleApproverActivity" + base.NodeName, new BookmarkCallback(OnResumeBookMark), BookmarkOptions.None);//如果为MultipleResume,则流程一直可以idel 
            }
            catch (Exception ex)
            {
                Log.Error("SingleApproverActivity.Execute", ex);
            }
        }

        public void OnResumeBookMark(NativeActivityContext context, Bookmark mark, object obj)
        {
            var wfid = context.WorkflowInstanceId;
            //获得业务数据的审批结果
            var wfInstance = db.t_WorkflowDyncStatus.SingleOrDefault(s => s.InstanceID == wfid);
            if (!wfInstance.IsNext.Value)
            {
                //如果是审批拒绝，则创建标签，等待审批
                context.CreateBookmark("SingleApproverActivity" + base.NodeName, new BookmarkCallback(OnResumeBookMark), BookmarkOptions.None);//如果为MultipleResume,则流程一直可以idel 
            }
            else
            {
                //赋值给参数
                base.Result.Set(context, wfInstance.IsNext.Value);
            }
        }
    }
}
